package net.koodar.forge.system.menu.service.assembler;

import net.koodar.forge.system.menu.model.dto.ManuParamDTO;
import net.koodar.forge.system.menu.model.entity.Menu;
import net.koodar.forge.system.menu.model.vo.MenuVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyc
 */
@Component
public class MenuAssembler {

    public Set<Map<String, String>> convertToPairPermissions(List<Menu> menus) {
        return menus
                .stream()
                .map(permission -> Map.of(
                        "label", permission.getName(),
                        "value", permission.getName()))
                .collect(Collectors.toSet());
    }

    public List<MenuVO> convertToTree(List<Menu> menus, List<Menu> allMenus) {
        List<MenuVO> menuVos = new ArrayList<>();

        if (menus == null) {
            menus = allMenus.stream()
                    .filter(permission -> permission.getParentId() == null || permission.getParentId() <= 0)
                    .toList();
        }

        for (Menu menu : menus) {
            MenuVO menuVo = convertToMenus(menu);
            List<Menu> childrenList = allMenus.stream()
                    .filter(p -> p.getParentId() != null && p.getParentId().equals(menu.getId()))
                    .collect(Collectors.toList());
            if (!menus.isEmpty()) {
                List<MenuVO> childrenMenus = convertToTree(childrenList, allMenus);
                childrenMenus.forEach(child -> child.setParentName(menu.getName()));
                menuVo.setChildren(childrenMenus);
            }
            menuVos.add(menuVo);
        }
        return menuVos;
    }

    private MenuVO convertToMenus(Menu menu) {
        MenuVO menuVo = new MenuVO();
        menuVo.setName(menu.getName());
        menuVo.setTitle(menu.getTitle());
        menuVo.setPath(menu.getPath());
        menuVo.setComponent(menu.getComponent());
        menuVo.setIcon(menu.getIcon());
        menuVo.setSort(menu.getSort());
        menuVo.setKeepAlive(menu.getKeepAlive());
        menuVo.setParentId(menu.getParentId());
        menuVo.setPerms(menu.getPerms());
        menuVo.setType(menu.getType());
        menuVo.setChildren(new ArrayList<>());
        return menuVo;
    }

    public Menu convertToMenu(ManuParamDTO manuParamDTO) {
        Menu menu = new Menu();
        menu.setParentId(manuParamDTO.getParentId());
        menu.setName(manuParamDTO.getName());
        menu.setTitle(manuParamDTO.getTitle());
        menu.setType(manuParamDTO.getType());
        menu.setIcon(manuParamDTO.getIcon());
        menu.setPerms(manuParamDTO.getPerms());
        menu.setPath(manuParamDTO.getPath());
        menu.setComponent(manuParamDTO.getComponent());
        menu.setSort(manuParamDTO.getSort());
        menu.setKeepAlive(manuParamDTO.getKeepAlive());
        menu.setId(manuParamDTO.getId());
        return menu;
    }

}
